<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          type="primary"
          @click="handleSearchList()"
          size="small"
        >
          查询搜索
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="small"
          label-width="140px"
        >
          <el-form-item label="线算名称：">
            <el-input
              v-model="listQuery.keyword"
              class="input-width"
              placeholder="题目名称"
              clearable
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button
        size="mini"
        class="btn-add"
        @click="handleAdd()"
        style="margin-left: 20px"
        >添加线算</el-button
      >
    </el-card>
    <div class="table-container">
      <el-table
        ref="rsaTable"
        :data="list"
        style="width: 100%"
        v-loading="listLoading"
        border
      >
        <el-table-column
          type="selection"
          width="60"
          align="center"
        ></el-table-column>
        <el-table-column label="编号" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="题目" align="center">
          <template slot-scope="scope">{{ scope.row.rsaName }}</template>
        </el-table-column>
        <el-table-column label="难度" align="center">
          <template slot-scope="scope">{{
            scope.row.rsaClass | formatClass
          }}</template>
        </el-table-column>
        <el-table-column label="创建时间" width="140" align="center">
          <template slot-scope="scope">{{
            scope.row.createTime | formatDate
          }}</template>
        </el-table-column>
        <el-table-column label="启用" width="200" align="center">
          <template slot-scope="scope">
            <el-switch
              @change="handleStatusChange(scope.$index, scope.row)"
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.rsaStatus"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.$index, scope.row)"
            >
              编辑
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleLinearList(scope.$index, scope.row)"
            >
              算法列表
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.$index, scope.row)"
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[5, 10, 15]"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      :title="isEdit ? '编辑线算' : '添加线算'"
      :visible.sync="dialogVisible"
      width="40%"
    >
      <el-form
        :model="rsaFuntionDataForm"
        ref="rsaFuntionForm"
        label-width="150px"
        size="small"
      >
        <el-form-item label="标题：">
          <el-input
            v-model="rsaFuntionDataForm.rsaName"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="类型：">
          <el-input
            v-model="rsaFuntionDataForm.rsaType"
            style="width: 300px"
            placeholder="例：行列式"
          ></el-input>
        </el-form-item>
        <el-form-item label="难 度：">
          <el-radio-group v-model="rsaFuntionDataForm.rsaClass">
            <el-radio :label="1">简单</el-radio>
            <el-radio :label="2">一般</el-radio>
            <el-radio :label="3">困难</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="启用">
          <el-radio-group v-model="rsaFuntionDataForm.rsaStatus">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="handleDialogConfirm()" size="small"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  fetchList,
  updateStatus,
  deleteRsaFuntion,
  createRsaFuntion,
  updateRsaFuntion,
} from "@/api/rsaFuntion";
import { formatDate } from "@/utils/date";

const defaultListQuery = {
  pageNum: 1,
  pageSize: 5,
  keyword: null,
};
const defaultRsaFuntion = {
  id: null,
  rsaName: null,
  rsaType: null,
  rsaClass: 0,
  createTime: null,
  updateTime: null,
  rsaStatus: 1,
  rsaNumber: null,
};
export default {
  name: "rsaFuntionList",
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: null,
      listLoading: false,
      dialogVisible: false,
      rsaFuntionDataForm: Object.assign({}, defaultRsaFuntion),
      isEdit: false,
    };
  },
  created() {
    this.getList();
  },
  filters: {
    formatClass(diff) {
      if (diff === 1) {
        return "简单";
      } else if (diff === 2) {
        return "一般";
      } else {
        return "困难";
      }
    },
    formatDate(time) {
      if (time == null || time === "") {
        return "N/A";
      }
      let date = new Date(time);
      return formatDate(date, "yyyy-MM-dd");
    },
  },
  methods: {
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
    },
    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    handleAdd() {
      this.dialogVisible = true;
      this.isEdit = false;
      this.rsaFuntionDataForm = Object.assign({}, defaultRsaFuntion);
    },
    handleStatusChange(index, row) {
      this.$confirm("是否要修改该状态?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          updateStatus(row.id, { status: row.rsaStatus }).then((response) => {
            if (response.code == 200) {
              this.$message({
                type: "success",
                message: "修改成功!",
              });
            } else {
              this.$message({
                type: "error",
                message: "修改失败!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消修改",
          });
          this.getList();
        });
    },
    handleDelete(index, row) {
      this.$confirm("是否要删除该活动?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteRsaFuntion(row.id).then((response) => {
          if (response.code == 200) {
            this.$message({
              message: "删除成功！",
              type: "success",
            });
          } else {
            this.$message({
              message: "删除失败！",
              type: "error",
            });
          }
          this.getList();
        });
      });
    },
    handleUpdate(index, row) {
      this.dialogVisible = true;
      this.isEdit = true;
      this.rsaFuntionDataForm = Object.assign({}, row);
    },
    handleLinearList(index, row) {
      console.log(row.id);
      this.$router.push({
        path: "/rsaFuntion/linearList",
        query: { rsaId: row.id },
      });
    },
    handleDialogConfirm() {
      this.$confirm("是否要确认?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        if (this.isEdit) {
          updateRsaFuntion(
            this.rsaFuntionDataForm.id,
            this.rsaFuntionDataForm
          ).then((response) => {
            this.$message({
              message: "修改成功！",
              type: "success",
            });
            this.dialogVisible = false;
            this.getList();
          });
        } else {
          createRsaFuntion(this.rsaFuntionDataForm).then((response) => {
            if (response.code == 200) {
              this.$message({
                message: "添加成功！",
                type: "success",
              });
            } else {
              this.$message({
                message: "添加失败！",
                type: "error",
              });
            }
            this.dialogVisible = false;
            this.getList();
          });
        }
      });
    },
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then((response) => {
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
      });
    },
  },
};
</script>
<style></style>


